package com.syx.extensiondemo.Controller;

import com.syx.extensiondemo.Constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("extension")
public class ProducerController {

    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;


    @Resource(name = "transRabbitTemplate")
    private RabbitTemplate transRabbitTemplate;

    //生产者
    @RequestMapping("/ack")
    public String ack(){

        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE,"ack","consume ack test");

        /*int a=3/0;*/
        return "消息发送成功";

    }



    //非持久化发送消息
    @RequestMapping("pres")
    public String pres(){
        Message message=new Message("test and test".getBytes(),new MessageProperties());
        //设置非持久化
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        rabbitTemplate.convertAndSend(Constants.PRES_EXCHANGE,"pres",message);
        return "消息发送成功";



    }
    @RequestMapping("/confirm")
    public String confirm(){
        //这种方式设置会导致所有rabbitTemplate被影响，重复调用接口的时候就会提示错误
        /*rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("执行confirm方法");
                if(ack==true){
                    System.out.println(correlationData.getId());

                }else {
                    System.out.printf("未接收到消息，原因是%s,消息id是 %s",cause,correlationData.getId());

                }
            }
        });*/
        //序号
        CorrelationData correlationData=new CorrelationData( "1");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm","confirm-test",correlationData);
        return "消息发送成功";

    }


    @RequestMapping("retry")
    public String retry(){
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE,"retry","hello retry");
        return "发送成功";

    }
    @RequestMapping("ttl")
    public String ttl(){
        System.out.println("设置ttl时间");
        //设置过期时间
        MessagePostProcessor messagePostProcessor=new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","hello ttl hello we are ttl",messagePostProcessor);
        return "发送ttl成功";



    }
    @RequestMapping("dl")
    public String dl(){
        System.out.println("设置ttl，但是不设置时间");
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","make dl greater again");
        return "发送ttl2成功";

    }

    @RequestMapping("dl2")
    public String dl2(){
        System.out.println("发送死信 ");
        for (int i = 0; i <20 ; i++) {
            rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","make dl greater again");
        }

        return "发送ttl2成功";

    }


    @RequestMapping("delay")
    public String delay(){
        System.out.println("延迟队列准备发送");

        MessagePostProcessor messagePostProcessor=new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelayLong(10000l);
                return message;
            }
        };
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","make delay greater again",messagePostProcessor);



        MessagePostProcessor messagePostProcessor1=new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelayLong(30000l);
                return message;
            }
        };

        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","make delay greater again",messagePostProcessor1);
        return "发送成功";

    }

    @Transactional
    @RequestMapping("trans")
    public String trans(){
        System.out.println("发送trans消息");
        //没有绑定的话就直接发给队列名字
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"发送trans消息1");

        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"发送trans消息2");
        return "success";


    }


    @RequestMapping("qos")
    public String qos(){
        System.out.println("直接发送消息");
        for (int i = 0; i < 200; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE,"qos","hello qos");

        }
        return "发送成功";


    }







}
